{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Self Driving Car 2D World [Demo]\n",
    "\n",
    "Now you'll use a 2-dimensional grid instead of a 1-dimensional grid. But you're going to do the same four things that you did for the 1-D case:\n",
    "* write a function that initializes probabilities across the grid\n",
    "* write a function that outputs the probability that the robot is at a specific point on the grid\n",
    "* write a function to visualize the probabilities of the grid (this function is provided for you)\n",
    "* write a function to update probabilities on the grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2D Grid Example\n",
    "\n",
    "Now, your robot lives in a two-dimensional world. Your robot can move left, right, forwards or backwards. Here is a bird's eye view of an example 7 by 7 grid.\n",
    "\n",
    "![alt text](2Dgrid.png \"2D Grid\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Review of a 1D Grid in Python\n",
    "\n",
    "In the 1D case, you made a list in Python with the probabilities like this:\n",
    "```python\n",
    "probability_list = [.2, .2, .2, .2, .2]\n",
    "```\n",
    "\n",
    "And then you accessed probabilities like this:\n",
    "```python\n",
    "probability_list[0]\n",
    "probability_list[1]\n",
    "probability_list[2]\n",
    "probability_list[3]\n",
    "probability_list[4]\n",
    "```\n",
    "\n",
    "This image below shows what the grid looks like and the associated index for each space.\n",
    "\n",
    "![alt text](1Dgrid_index.png \"2D Grid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2D Grid in Python\n",
    "\n",
    "\n",
    "Now, with a 2D grid, each space needs two indices: one index is for the row and the other index for the column.\n",
    "\n",
    "![alt text](2Dgrid_index.png \"2D Grid\")\n",
    "\n",
    "The first index represents the row, and the second index represents the column. In Python, a 2D grid could be represented with a nested list like this.\n",
    "\n",
    "```python\n",
    "probability_grid = [[1/15, 1/15, 1/15, 1/15, 1/15],\n",
    "                    [1/15, 1/15, 1/15, 1/15, 1/15],\n",
    "                    [1/15, 1/15, 1/15, 1/15, 1/15]]\n",
    "\n",
    "```\n",
    "\n",
    "To access these values, you need the row number and the column number. The values in the first row are\n",
    "```python\n",
    "probability_grid[0][0]\n",
    "probability_grid[0][1]\n",
    "probability_grid[0][2]\n",
    "probability_grid[0][3]\n",
    "probability_grid[0][4]\n",
    "```\n",
    "\n",
    "The values in the second row are\n",
    "```python\n",
    "probability_grid[1][0]\n",
    "probability_grid[1][1]\n",
    "probability_grid[1][2]\n",
    "probability_grid[1][3]\n",
    "probability_grid[1][4]\n",
    "```\n",
    "\n",
    "And the third row\n",
    "```python\n",
    "probability_grid[2][0]\n",
    "probability_grid[2][1]\n",
    "probability_grid[2][2]\n",
    "probability_grid[2][3]\n",
    "probability_grid[2][4]\n",
    "```\n",
    "\n",
    "# Code Examples\n",
    "\n",
    "Now, run the code cell below. It contains a few examples of how to use nested for loops. These code examples should help you with the coding exercises.\n",
    "\n",
    "### Example 1\n",
    "\n",
    "Print out the values in a nested list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "print out list values one at a time\n",
      "5\n",
      "3\n",
      "9\n",
      "7\n",
      "6\n",
      "2\n",
      "7\n",
      "2\n",
      "1\n",
      "9\n",
      "8\n",
      "5\n",
      "3\n",
      "1\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "nested_list = [[5, 3, 9, 7, 6],\n",
    "               [2, 7, 2, 1, 9],\n",
    "              [8, 5, 3, 1, 8]]\n",
    "\n",
    "def print_list(nested_list):\n",
    "    print('print out list values one at a time')\n",
    "    for i in range(len(nested_list)):\n",
    "        for j in range(len(nested_list[0])):\n",
    "            print(nested_list[i][j])\n",
    "\n",
    "print_list(nested_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 2\n",
    "\n",
    "Print out the values in a nested list with some nicer formatting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "print out list values with formatting\n",
      "5, 3, 9, 7, 6\n",
      "2, 7, 2, 1, 9\n",
      "8, 5, 3, 1, 8\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def print_formatted_list(nested_list):\n",
    "    # print a blank line\n",
    "    print('\\nprint out list values with formatting')\n",
    "    # print out list values with formatting\n",
    "    for i in range(len(nested_list)):\n",
    "        for j in range(len(nested_list[0])):\n",
    "            # if statement makes sure the last number in a row does not have a comma\n",
    "            if j != len(nested_list[0]) - 1:\n",
    "                print(str(nested_list[i][j]) + \", \" , end=\"\")\n",
    "            else:\n",
    "                print(str(nested_list[i][j]), end=\"\")\n",
    "\n",
    "        print()\n",
    "    print()\n",
    "    \n",
    "print_formatted_list(nested_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example Three\n",
    "\n",
    "This example shows you how to create a 2D list and append new rows to the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "print out list values with formatting\n",
      "5, 2, 1, 8\n",
      "5, 2, 1, 8\n",
      "5, 2, 1, 8\n",
      "5, 2, 1, 8\n",
      "5, 2, 1, 8\n",
      "\n"
     ]
    }
   ],
   "source": [
    "twodlist = []\n",
    "number_rows = 5\n",
    "\n",
    "for i in range(number_rows):\n",
    "    twodlist.append([5, 2, 1, 8])        \n",
    "\n",
    "print_formatted_list(twodlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example Four\n",
    "\n",
    "For the last example, here is a 2D list created with a nested for loop. Notice how the code first create a new row and then appends the row to the 2D list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "print out list values with formatting\n",
      "0, 0, 0, 0, 0, 0\n",
      "1, 1, 1, 1, 1, 1\n",
      "2, 2, 2, 2, 2, 2\n",
      "3, 3, 3, 3, 3, 3\n",
      "4, 4, 4, 4, 4, 4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "twodlist = []\n",
    "row = []\n",
    "number_rows = 5\n",
    "number_columns = 6\n",
    "\n",
    "for i in range(number_rows):\n",
    "    for j in range(number_columns):\n",
    "        row.append(i)\n",
    "    twodlist.append(row)\n",
    "    row = []\n",
    "\n",
    "print_formatted_list(twodlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercises\n",
    "\n",
    "Go on to the next part of the lesson to start the exercises!\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
